package com.yogee.topseller.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.my.OrderDetailsActivity;
import com.yogee.topseller.bean.ResultBean;
import com.yogee.topseller.bean.SubscribeBean;
import com.yogee.topseller.http.HttpManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目名称：TopSeller
 * 类描述：
 * 创建时间：2017/7/3 0003 14:27
 * 创建人：wangye
 * 修改备注：
 */
public class SubscriptionAdapter extends RecyclerView.Adapter<SubscriptionAdapter.MyViewHolder> {
    private List<SubscribeBean.ProListBean> listBean;
    private OnYYItemClickListener onYYItemClickListener;

    public void setListBean(List<SubscribeBean.ProListBean> listBean, int type) {
        this.listBean = listBean;
        this.type = type;
        notifyDataSetChanged();
    }

    public void setOnYYItemClickListener(OnYYItemClickListener onYYItemClickListener) {
        this.onYYItemClickListener = onYYItemClickListener;
    }

    private Context context;
    private int type;

    public SubscriptionAdapter(Context context) {
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_subscription, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        if (0 == type) {
            holder.itemLook.setVisibility(View.GONE);
            holder.itemReservation.setVisibility(View.VISIBLE);
            holder.itemReservation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onYYItemClickListener.onYJClick(listBean.get(position).getId());
                }
            });
            holder.yuyueTv.setVisibility(View.VISIBLE);
            if (listBean.get(position).getIsAuto().equals("0")) {
                holder.yuyueImg.setImageResource(R.drawable.unselect1);
                holder.yuyueTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onYYItemClickListener.onAutoClick(listBean.get(position).getId(),0);
                    }
                });
            }else{
                holder.yuyueImg.setImageResource(R.drawable.select1);
                holder.yuyueTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onYYItemClickListener.onAutoClick(listBean.get(position).getId(),1);
                    }
                });
            }
        } else {
            holder.itemLook.setVisibility(View.VISIBLE);
            holder.itemReservation.setVisibility(View.GONE);
            holder.itemLook.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    context.startActivity(new Intent(context, OrderDetailsActivity.class).putExtra("proId", listBean.get(position).getId()));
                }
            });

        }
        holder.commission.setText(listBean.get(position).getExpectReturn());
        holder.expectReturn.setText(listBean.get(position).getExpectReturn().replace("%",""));
        holder.startAmount.setText(listBean.get(position).getStartAmount());
        holder.leftDate.setText(listBean.get(position).getLeftDate());
        holder.name.setText(listBean.get(position).getName());
        holder.subDate.setText(listBean.get(position).getSubDate());
        holder.subMoney.setText(listBean.get(position).getSubMoney());
        holder.time.setText(listBean.get(position).getTime());
        holder.type.setText(listBean.get(position).getProType());
        holder.yongjin.setText(listBean.get(position).getCommissionYJ());
        holder.tv_pro.setText(listBean.get(position).getPercent()+"%");
        holder.pb_pro.setProgress(Integer.valueOf(listBean.get(position).getPercent()));

    }

    @Override
    public int getItemCount() {
        return listBean == null ? 0 : listBean.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_reservation)
        RelativeLayout itemReservation;
        @BindView(R.id.item_look)
        RelativeLayout itemLook;
        @BindView(R.id.item_subscription_button)
        RelativeLayout itemSubscriptionButton;
        @BindView(R.id.leftDate)
        TextView leftDate;
        @BindView(R.id.time)
        TextView time;
        @BindView(R.id.subDate)
        TextView subDate;
        @BindView(R.id.subMoney)
        TextView subMoney;
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.startAmount)
        TextView startAmount;
        @BindView(R.id.commission)
        TextView commission;
        @BindView(R.id.expectReturn)
        TextView expectReturn;
        @BindView(R.id.item_subscription_yuyue)
        LinearLayout yuyueTv;
        @BindView(R.id.item_subscription_yuyue_img)
        ImageView yuyueImg;

        @BindView(R.id.type)
        TextView type;
        @BindView(R.id.img_yu)
        ImageView img_yu;
        @BindView(R.id.img_tu)
        ImageView img_tu;
        @BindView(R.id.yongjin)
        TextView yongjin;
        @BindView(R.id.tv_pro)
        TextView tv_pro;
        @BindView(R.id.pb_pro)
        ProgressBar pb_pro;


        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnYYItemClickListener {
        void onYJClick(String orderId);

        void onAutoClick(String orderId,int type);
    }
}